﻿using System.Collections.Generic;
using System.Windows.Media;

namespace MyFloridaHouse.libSvUtilities.CustomColors
{
    // Here is an interesting well written article explaining color usage
    // http://www.colortools.net/article_web_colors.html

    public class WebSafeColors : ICustomColors
    {
        public WebSafeColors()
        {
            GenerateAllPossibleWebSafeColors();
            GenerateRandomIndexes();
        }

        #region ICustomColors

        private List<Color> _colorList = new List<Color>();
        public List<Color> colorList
        {
            get { return _colorList; }
            set
            {
                if (_colorList != value)
                {
                    _colorList = value;
                }
            }
        }

        public Color GetColorFromIndex(int index, double alpha)
        {
            int reassignmentIndex = index;
            if (reassignmentIndex < 0)
            {
                reassignmentIndex = 0;
            }
            else if (reassignmentIndex >= colorIndexList.Count)
            {
                // wrap around to a valid color
                reassignmentIndex = colorIndexList.Count % index;
            }

            return colorList[colorIndexList[reassignmentIndex]];
        }

        #endregion ICustomColors

        private List<int> _colorIndexList = new List<int>();
        public List<int> colorIndexList
        {
            get { return _colorIndexList; }
            set
            {
                if (_colorIndexList != value)
                {
                    _colorIndexList = value;
                }
            }
        }

        // web safe colors are made up of these hex numbers in various combinations of these 6 numbers: 00, 33, 66, 99, CC, FF
        // There are 216 posible combinations (6*6*6=216) in the web safe color palatte
        // this shows the safe colors and their hex numbers http://www.colortools.net/color_chart_web-safe.html

        private void GenerateAllPossibleWebSafeColors()
        {
            string vals = "FF,CC,99,66,33,00";
            string[] valArray = vals.Split(',');

            for (int rr = 0; rr < 6; rr++)
            {
                for (int gg = 0; gg < 6; gg++)
                {
                    for (int bb = 0; bb < 6; bb++)
                    {
                        colorList.Add(RGB.GetColorFromHex(.6, valArray[rr] + valArray[gg] + valArray[bb]));
                    }
                }
            }
        }

        private void GenerateRandomIndexes()
        {

            // this bizarre mapping comes by printing out the colors in 6(red) rows of 6(green) by 6(blue) grids (for the 6 basic web safe elements "FF,CC,99,66,33,00")
            // and examining the patterns.  The colors generally alter only slightly as you move from one index to the next.
            // To get contrasting side-by-side colors, you have to jump around, so this scheme does that.
            // Basically, this re-orders the color indexes in a somewhat random pattern

            colorIndexList.Clear();

            int[] redIndex = new int[12] { 0, 4, 2, 5, 1, 3, 2, 0, 1, 3, 4, 5 };
            int greenTopOffset = 0;
            int greenBottomOffset = 3;

            for (int greenIndex = 0; greenIndex < 3; greenIndex++)
            {
                for (int blueIndex = 0; blueIndex < 6; blueIndex++)
                {
                    int ndx = 0;
                    colorIndexList.Add(redIndex[ndx++] * 36 + (greenTopOffset + greenIndex) * 6 + blueIndex);
                    colorIndexList.Add(redIndex[ndx++] * 36 + (greenTopOffset + greenIndex) * 6 + blueIndex);
                    colorIndexList.Add(redIndex[ndx++] * 36 + (greenBottomOffset + greenIndex) * 6 + blueIndex);

                    colorIndexList.Add(redIndex[ndx++] * 36 + (greenTopOffset + greenIndex) * 6 + blueIndex);
                    colorIndexList.Add(redIndex[ndx++] * 36 + (greenBottomOffset + greenIndex) * 6 + blueIndex);
                    colorIndexList.Add(redIndex[ndx++] * 36 + (greenTopOffset + greenIndex) * 6 + blueIndex);

                    colorIndexList.Add(redIndex[ndx++] * 36 + (greenTopOffset + greenIndex) * 6 + blueIndex);
                    colorIndexList.Add(redIndex[ndx++] * 36 + (greenBottomOffset + greenIndex) * 6 + blueIndex);
                    colorIndexList.Add(redIndex[ndx++] * 36 + (greenTopOffset + greenIndex) * 6 + blueIndex);

                    // these sectins should be unused because the colors are too dark
                    colorIndexList.Add(redIndex[ndx++] * 36 + (greenBottomOffset + greenIndex) * 6 + blueIndex);
                    colorIndexList.Add(redIndex[ndx++] * 36 + (greenBottomOffset + greenIndex) * 6 + blueIndex);
                    colorIndexList.Add(redIndex[ndx++] * 36 + (greenBottomOffset + greenIndex) * 6 + blueIndex);
                }
            }
        }


    }
}
